package com.laowang.project.system.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.laowang.common.exception.BaseException;
import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.DateUtils;
import com.laowang.common.utils.IdWorker;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.common.utils.StringUtils;
import com.laowang.framework.security.LoginUser;
import com.laowang.framework.security.service.TokenService;
import com.laowang.project.pay.config.WxPayConfigUtil;
import com.laowang.project.system.domain.MyCyhHolderVipNum;
import com.laowang.project.system.domain.TbAccountDetail;
import com.laowang.project.system.domain.vo.WithdrawObj;
import com.laowang.project.system.service.TbAccountDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.WxUserMapper;
import com.laowang.project.system.domain.WxUser;
import com.laowang.project.system.service.WxUserService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;

/**
 * 用户信息Service业务层处理
 *
 * @author laowang
 * @date 2022-02-03
 */

@Slf4j
@Service
public class WxUserServiceImpl implements WxUserService {
    @Autowired
    private WxUserMapper wxUserMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TbAccountDetailService tbAccountDetailService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询用户信息
     *
     * @param mid 用户信息ID
     * @return 用户信息
     */
    @Override
    public WxUser selectWxUserById(Integer mid) {
        return wxUserMapper.selectWxUserById(mid);
    }

    /**
     * 查询用户信息列表
     *
     * @param wxUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<WxUser> selectWxUserList(WxUser wxUser) {
        return wxUserMapper.selectWxUserList(wxUser);
    }

    /**
     * 新增用户信息
     *
     * @param wxUser 用户信息
     * @return 结果
     */
    @Override
    public int insertWxUser(WxUser wxUser) {
        wxUser.setCreateTime(DateUtils.getNowDate());
        System.out.println(wxUser);
        return wxUserMapper.insertWxUser(wxUser);
    }

    /**
     * 修改用户信息
     *
     * @param wxUser 用户信息
     * @return 结果
     */
    @Override
    public int updateWxUser(WxUser wxUser) {
        if (wxUser.getStockHolder() != null && wxUser.getStockHolder().equals(1)) {

            if (!wxUser.getMid().equals(wxUser.getLine())) {
                wxUser.setLine(wxUser.getMid());
            }

        } else if (  wxUser.getIsPartner() != null && wxUser.getIsPartner().equals(1)) {
            if (wxUser.getExpireTime2() == null || wxUser.getPartnerVip() ==null){
                throw new CustomException("合伙人级别,到期时间请填写完整");
            }

            if (!wxUser.getMid().equals(wxUser.getLineTwo())) {
                wxUser.setLineTwo(wxUser.getMid());
            }

        } else if ( wxUser.getPromoDor() != null && wxUser.getPromoDor().equals(1)) {
            if (wxUser.getPromoLevel() == null || wxUser.getExpireTime1()== null){
                throw new CustomException("推荐官级别,到期时间请填写完整");
            }

            if (!wxUser.getMid().equals(wxUser.getLineThree())) {
                wxUser.setLineThree(wxUser.getMid());
            }

        }

        return wxUserMapper.updateWxUser(wxUser);
    }

    /**
     * 批量删除用户信息
     *
     * @param mids 需要删除的用户信息ID
     * @return 结果
     */
    @Override
    public int deleteWxUserByIds(Long[] mids) {
        return wxUserMapper.deleteWxUserByIds(mids);
    }

    /**
     * 删除用户信息信息
     *
     * @param mid 用户信息ID
     * @return 结果
     */
    @Override
    public int deleteWxUserById(Long mid) {
        return wxUserMapper.deleteWxUserById(mid);
    }

    @Override
    public WxUser getByOpenid(String openid) {
        return wxUserMapper.getByOpenid(openid);
    }

    @Override
    public Integer registerNum() {
        return wxUserMapper.registerNum();
    }

    @Override
    public List<WxUser> selectWxUserListWeb(WxUser wxUser1) {
        return wxUserMapper.selectWxUserListWeb(wxUser1);
    }

    @Override
    public List<WxUser> selectWxUserListNoIntroMid() {
        return wxUserMapper.selectWxUserListNoIntroMid();
    }

    @Override
    public List<WxUser> selectWxUserListExport(WxUser wxUser) {
        return wxUserMapper.selectWxUserListExport(wxUser);
    }

    @Override
    public List<WxUser> selectWxUserListExport1(WxUser wxUser) {
        return wxUserMapper.selectWxUserListExport1(wxUser);
    }

    @Override
    public void updateWxUserNull(WxUser wxUser1) {
        wxUserMapper.updateWxUserNull(wxUser1.getMid());
    }

    @Override
    public List selectWxUserListTuanDui(WxUser wxUser1) {
        return wxUserMapper.selectWxUserListTuanDui(wxUser1);
    }

    @Override
    public List selectWxUserListStructure(WxUser wxUser1) {
        return wxUserMapper.selectWxUserListStructure(wxUser1);
    }

    @Override
    public List<MyCyhHolderVipNum> selectListByUser(WxUser wxUser) {
        return wxUserMapper.selectListByUser(wxUser);
    }

    @Override
    public WxUser selectByPhone(String phone) {
        return wxUserMapper.selectByPhone(phone);
    }


}
